'use strict';

const Controller = require('egg').Controller;
const {ObjectId}=require('mongodb')
class SubjectController extends Controller {
    async index() {
        const { ctx } = this;
        const {id} = ctx.params
        try{
            const db = this.app.mongodb;
            const res = await db.collection('article').find({_id:ObjectId(id)}).toArray()
            var url = ctx.origin+"/public"
            res.forEach(item => {
                item.imgSrc = url + item.imgSrc
                item.avatar = url + item.avatar
                item.headImgSrc = url + item.headImgSrc
            });
            ctx.body = {
                code:200,
                res
            }
        }catch(err){
            this.app.logger.error(err)
            ctx.status = 400
            ctx.body={
                status:400,
                msg:"输入的id值参数不合法"
            }
        }
    }
}

module.exports = SubjectController;
